package cn.zdm.demo2.auth.controller;

import cn.zdm.demo2.auth.model.dto.AuthDTO;
import cn.zdm.demo2.auth.model.po.IosAppAuthPO;
import cn.zdm.demo2.auth.model.po.IosAppPO;
import cn.zdm.demo2.auth.service.IosAppAuthService;
import cn.zdm.demo2.auth.service.IosAppService;
import cn.zdm.demo2.common.log.LogOpt;
import cn.zdm.demo2.common.result.ResultObject;
import cn.zdm.demo2.common.result.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * copyright (C), 2021, 武大吉奥科技有限公司
 *
 * @Program demo2
 * @Description
 * @Author ZhaoDeman
 * @Create 2021/1/19 23:27
 * @Version 1.0.0
 * <author>                <time>                  <version>                   <description>
 * ZhaoDeman        2021/1/19 23:27                 1.0
 */
@RestController
@Api(tags = "系统扩展")
@RequestMapping("/ios/auth/app")
@Slf4j
public class AppController {
    @Autowired
    private IosAppService iosAppService;

    @Autowired
    private IosAppAuthService iosAppAuthService;

    @ApiOperation(value = "查询授权列表")
    @GetMapping("/queryAuth/{appId}/{authType}/{authName}/{state}/{isOverShoot}/{pageNo}/{pageSize}/{gsVer}/{authCk}/{lgUser}/{method}/{cookie}/{timeStamp}")
    //@LogOpt(desc = "查询授权列表")
    public ResultObject<Object> queryAuth(@PathVariable(value = "appId") String appId,
                                          @PathVariable(value = "authType") String authType,
                                          @PathVariable(value = "authName") String authName,
                                          @PathVariable(value = "state") String state,
                                          @PathVariable(value = "isOverShoot") String isOverShoot,
                                          @PathVariable(value = "pageNo") Integer pageNo,
                                          @PathVariable(value = "pageSize") Integer pageSize,
                                          @PathVariable(value = "gsVer") String gsVer,
                                          @PathVariable(value = "authCk") String authCk,
                                          @PathVariable(value = "lgUser") String lgUser,
                                          @PathVariable(value = "method") String method,
                                          @PathVariable(value = "cookie") String cookie,
                                          @PathVariable(value = "timeStamp") String timeStamp) {
        AuthDTO authDTO = AuthDTO.builder().appId(appId)
                .authType(authType).authName(authName)
                .state(state).isOverShoot(isOverShoot)
                .pageNo(pageNo).pageSize(pageSize)
                .gsVersion(gsVer).authCk(authCk)
                .lgUser(lgUser).method(method)
                .cookie(cookie).timeStamp(timeStamp).build();
        List<IosAppAuthPO> result = iosAppAuthService.queryIosAuthByPage(authDTO);
        return ResultResponse.success("查询成功", result.size(), result);
    }

    @ApiOperation(value = "查询授权列表测试接口2")
    @GetMapping("/queryAuth/")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用编号", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "authType", value = "权限类型", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "authName", value = "权限名", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "state", value = "状态", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "isOverShoot", value = "应用状态", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "分页条数", dataType = "Integer", paramType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "String", required = true),
            @ApiImplicitParam(name = "gsVer", value = "服务接口版本", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "authCk", value = "authCk", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "lgUser", value = "lgUser", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "method", value = "method", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "cookie", value = "cookie", dataType = "String", paramType = "String"),
            @ApiImplicitParam(name = "timeStamp", value = "时间戳", dataType = "String", paramType = "String"),
    })
    //@LogOpt(desc = "查询授权列表")
    public ResultObject<Object> queryAuthList(AuthDTO authDTO) {
        log.info("====>{}", authDTO.toString());
        List<IosAppAuthPO> result = iosAppAuthService.queryIosAuthByPage(authDTO);
        return ResultResponse.success("查询成功", result.size(), result);
    }

    @ApiOperation(value = "添加权限信息")
    @PostMapping("/add/auth")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "authType", value = "权限类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authName", value = "权限接口", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authLoseTime", value = "权限失效时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "authMaxRows", value = "最大条数", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "authTimeSpan", value = "时间频率", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "haveVisit", value = "已经访问条数", dataType = "Integer", paramType = "query")
    })*/
    //@LogOpt(desc = "系统扩展，访问添加权限信息")
    public ResultObject<Object> addAuth(@RequestBody IosAppAuthPO iosAppAuthPO) {
        log.info("=====>{}", iosAppAuthPO.toString());
        return iosAppAuthService.saveAppAuth(iosAppAuthPO) > 0 ? ResultResponse.success("权限添加成功") : ResultResponse.error("500", "权限信息添加失败");
    }

    @ApiOperation(value = "测试接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "参数", dataType = "String", paramType = "query", required = true),
    })
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    //@LogOpt(desc = "系统扩展，测试接口")
    public ResultObject<Object> test() {

        //name 对应字段名 value 参数说明 dataType对应于传递的参数的类型 paramType query相当于@RequestParam
        return ResultResponse.success();
    }

    @ApiOperation(value = "批量添加app信息")
    @RequestMapping(value = "/save/app", method = RequestMethod.GET)
    @LogOpt(desc = "批量添加app信息")
    public ResultObject<Object> saveApp() {
        // List<IosAppPO> list = new ArrayList<>();
        IosAppPO iosAppPO;
        for (int i = 10; i < 20; i++) {
            iosAppPO = new IosAppPO();
            iosAppPO.setAppCreator("zhaodeman");
            iosAppPO.setAppName("应用" + i);
            iosAppPO.setAppLoseTime(new Date());
            iosAppPO.setAppCreateTime(new Date());
            iosAppService.saveIosApp(iosAppPO);
            // list.add(iosAppPO);
        }
        return ResultResponse.success("批量添加app信息成功");
    }

    @ApiOperation(value = "添加应用app")
    @PostMapping(value = "/insert/app")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "appName", value = "应用名", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appCreator", value = "创建者", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appCreateTime", value = "创建时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "appLoseTime", value = "失效时间", dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "appEncryptType", value = "应用加密类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appDecryptType", value = "应用解密类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appEncodeType", value = "应用编码类型", dataType = "String", paramType = "query")
    })*/
    //@LogOpt(desc = "系统扩展，插入app")
    public ResultObject<Object> insertApp(@RequestBody IosAppPO IosAppPO) {
        return iosAppService.saveIosApp(IosAppPO) > 0 ? ResultResponse.success("添加成功") : ResultResponse.error("添加失败");
    }

    @ApiOperation(value = "查询app列表")
    @GetMapping("/query/app")
    public ResultObject<Object> queryApp() {
        List<IosAppPO> result = iosAppService.queryIosApp();
        log.info("====>{}", result.toString());
        return ResultResponse.success("查询成功", result);
    }

    @ApiOperation(value = "删除应用")
    @GetMapping("/app/del")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true)
    })
    public ResultObject<Object> delApp(String appId) {
        if (iosAppService.delIosApp(appId)) {
            return ResultResponse.success("删除成功！");
        }
        return ResultResponse.error("删除失败，请检查appID");
    }

    @ApiOperation(value = "删除权限")
    @GetMapping("/auth/del")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authId", value = "authId", dataType = "String", paramType = "query", required = true)
    })
    public ResultObject<Object> delAppAuth(String authId) {


        if (iosAppAuthService.delIosAuth(authId)) {
            return ResultResponse.success("删除成功！");
        }
        return ResultResponse.error("删除失败，请检查authId");


    }
}
